package com.Final.hj;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ChatPageAdapter extends BaseAdapter{

    private int chat_page_id;
    private Context cContent;
    private List<ChatPageData> chat_lists;
    private View view;
    private String myId;

    private LayoutInflater InflaterItem;//要显示的布局容器

    public ChatPageAdapter(Context content, List<ChatPageData> chatLists) {
        super();
        cContent = content;
        this.chat_lists = chatLists;

        //获取布局容器
        InflaterItem = LayoutInflater.from(content);

        this.myId = LoginSQLManger.getInstance(content).queryAll().getMyId();
    }

    public static interface IMsgViewType {
        int IMVT_COM_MSG = 0;// 收到对方的消息
        int IMVT_TO_MSG = 1;// 自己发送出去的消息
    }


    public int getItemViewType(int position) {
        ChatPageData entity = chat_lists.get(position);

        if (myId.equals(entity.getMsgId())) {// 自己发送的消息
            return IMsgViewType.IMVT_TO_MSG;//1
        } else {// 收到的消息
            return IMsgViewType.IMVT_COM_MSG;//0
        }
    }

    //重写四个方法
    @Override
    //ListView需要显示的数据数量
    public int getCount() {
        return chat_lists.size();
    }

    @Override
    //指定的索引对应的数据项
    public Object getItem(int position) {
        return chat_lists.get(position);
    }

    @Override
    //指定的索引对应的数据项ID
    public long getItemId(int position) {
        return position;
    }


    @Override
    //返回每一项的显示内容
    public View getView(int position, View itemView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        //这里即为我们要的数据  取出数据
        ChatPageData chat_list_item = chat_lists.get(position);

        //如果view未被实例化过，缓存池中没有对应的缓存
        if (viewHolder == null) {
            viewHolder = new ViewHolder();

            //判断是否是我自己发送的消息
            if(myId.equals(chat_list_item.getMsgId())){//我发的消息

                //获取布局文件
                itemView = View.inflate(cContent, R.layout.chat_item_right,
                        null);

                //初始化变量
                viewHolder.me_Image = (ImageView) itemView.findViewById(R.id.chat_me_himg);
                viewHolder.me_msg = (TextView) itemView.findViewById(R.id.chat_me_message);
                viewHolder.me_time = (TextView) itemView.findViewById(R.id.chat_me_time);

                viewHolder.me_Image.setImageResource(Integer.parseInt(chat_list_item.gethImgId()));
                viewHolder.me_msg.setText(chat_list_item.getMsg());
                viewHolder.me_time.setText(chat_list_item.getMsgTime());
            }else{//对方发送的消息

                //获取布局文件
                itemView = View.inflate(cContent, R.layout.chat_item_left,
                        null);

                //初始化变量
                viewHolder.he_Image = (ImageView) itemView.findViewById(R.id.chat_he_himg);
                viewHolder.he_msg = (TextView) itemView.findViewById(R.id.chat_he_message);
                viewHolder.he_time = (TextView) itemView.findViewById(R.id.chat_he_time);

                viewHolder.he_Image.setImageResource(Integer.parseInt(chat_list_item.gethImgId()));
                viewHolder.he_msg.setText(chat_list_item.getMsg());
                viewHolder.he_time.setText(chat_list_item.getMsgTime());
            }

            //通过setTag将convertView与viewHolder关联
            itemView.setTag(viewHolder);
        }else{
            //如果缓存池中有对应的view缓存，则直接通过getTag取出viewHolder
            viewHolder = (ViewHolder) itemView.getTag();
        }

        return itemView;
    }


    //这里即为布局相应的 控件
    class ViewHolder{
        //我的
        public ImageView me_Image;
        public TextView me_msg;
        public TextView me_time;

        //他的
        public ImageView he_Image;
        public TextView he_msg;
        public TextView he_time;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
}
